#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

const int Mod = 1e9 + 7;
int qpow(int a, int b)
{
    int ret = 1;
    while (b > 0)
    {
        if (b & 1)
            ret = ret * a % Mod;
        a = a * a % Mod;
        b >>= 1;
    }
    return ret;
}

void solve()
{
    int l, r, y;
    cin >> l >> r >> y;
    if (l == r)
    {
        int cnt = l ^ y;
        int ans = __builtin_popcountll(cnt);
        cout << ans << endl;
        return;
    }
    int rr = r;
    int ma = 0;
    while (rr)
    {
        rr >>= 1;
        ma++;
    }
    int ans = 0;
    for (int k = 0; k < ma; k++)
    {
        int x = (y & 1) ? 1 : 0;
        int rsum = ((r + 1) / (1 << (k + 1)) * (1 << k) % Mod + max(0LL, ((r + 1) % (1 << (k + 1))) - (1 << k))) % Mod;
        int lsum = (l / (1 << (k + 1)) * (1 << k) % Mod + max(0LL, (l % (1 << (k + 1))) - (1 << k))) % Mod;
        int cnt = rsum - lsum;
        if (x) ans = ((ans + (r - l + 1)) % Mod - cnt + Mod) % Mod;
        else ans = (ans + cnt) % Mod;
        y >>= 1;
    }
    //cout << ans << endl;
    ans = (ans * qpow(r - l + 1, Mod - 2)) % Mod;
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}